package io.bhex.sdk.security.utils;

import android.app.Activity;
import android.app.Dialog;
import android.os.Build;
import android.view.Window;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * ================================================
 * 作   者：ppzhao
 * 创建时间：2019-11-12
 * 邮   箱：
 * 描   述：
 * ================================================
 */

public class NoScreenShotAndRecord {

    private static NoScreenShotAndRecord instance;
    private static List<Object> activityList;

    public static NoScreenShotAndRecord getInstance(){
        if (instance == null) {
            instance = new NoScreenShotAndRecord();
            activityList = new ArrayList<Object>();
        }
        return instance;
    }


    /**
     * 禁止页面截屏和录屏
     * @param activity
     */
    public static void forbidPageShotAndRecord(Activity activity){
        if (!activityList.contains(activity)) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
            activityList.add(activity);
        }
    }

    /**
     * 禁止页面截屏和录屏
     * @param activity
     */
    public static void forbidShotAndRecord(Activity activity){
        if (!activityList.contains(activity)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
                Window window = activity.getWindow();
                WindowManager wm = activity.getWindowManager();
//                wm.removeViewImmediate(window.getDecorView());
//                wm.addView(window.getDecorView(), window.getAttributes());
                wm.updateViewLayout(window.getDecorView(), window.getAttributes());
                activityList.add(activity);
            }
        }
    }

    /**
     * 禁止Dialog截屏和录屏
     * @param dialog
     */
    public static void forbidDialogShotAndRecord(Dialog dialog){
        if (!activityList.contains(dialog)) {
            dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
                    WindowManager.LayoutParams.FLAG_SECURE);
            activityList.remove(dialog);
        }
    }

    public void unforbidShotAndRecord(Dialog dialog) {
        if (activityList.contains(dialog)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
                activityList.remove(dialog);
            }
        }
    }

    public void unforbidShotAndRecord(Activity activity) {
        if (activityList.contains(activity)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
                activityList.remove(activity);
            }
        }
    }
}
